
//定义一个坦克基类
class Tank {
  constructor(x, y, direct, color) {
    this.x = x
    this.y = y
    this.speed = 1
    this.direct = direct
    this.color = color
  }
  moveUp() {
    if (this.y <= 0) {
      this.y = this.y
      this.direct = 0
    }
    else {
      this.y -= this.speed
      this.direct = 0
    }
  }
  moveRight() {
    if (this.x >= 352) {
      this.x = this.x
      this.direct = 1
    }
    else {
      this.x += this.speed
      this.direct = 1
    }
  }
  moveDown() {
    if (this.y >= 352) {
      this.y = this.y
      this.direct = 2
    }
    else {
      this.y += this.speed
      this.direct = 2
    }
  }
  moveLeft() {
    if (this.x <= 0) {
      this.x = this.x
      this.direct = 3
    }
    else {
      this.x -= this.speed
      this.direct = 3
    }
  }
}

var hero

//定义一个敌人坦克的空数组
var enemyTanks = new Array()

//定义一个英雄子弹，刚开始为空
var heroBullet = null

//定义一个子弹空数组
var heroBullets = new Array()

//定义一个坦克英雄类（继承父亲基类）
class Hero extends Tank {
  constructor(x, y, direct, color) {
    super(x, y, direct, color)
    this.liver = true
  }
  shotBullet() {
    heroBullet = new Bullet(this.x, this.y, this.direct)
    heroBullets.push(heroBullet)
    var timer = window.setInterval("heroBullets[" + (heroBullets.length - 1) + "].run()", 10)
    heroBullets[heroBullets.length - 1].timer = timer
  }

  dead() {
    for (var i = 0; i < enemyTankBullets.length; i++) {
      if (enemyTankBullets[i].liver == true) {
        switch (enemyTankBullets[i].direct) {
          case 0:
            if ((this.x + 4 <= enemyTankBullets[i].x + 15 && enemyTankBullets[i].x + 15 <= this.x - 4 + 32) && (this.y + 4 <= enemyTankBullets[i].y && enemyTankBullets[i].y <= this.y - 4 + 32)) {
              if (this.liver == true) {
                enemyTankBullets[i].liver = false
                clearInterval(enemyTankBullets[i].timer)
              }
              this.liver = false
            }
            break
          case 1:
            if ((this.x + 4 <= enemyTankBullets[i].x + 32 && enemyTankBullets[i].x + 32 <= this.x - 4 + 32) && (this.y + 4 <= enemyTankBullets[i].y + 15 && enemyTankBullets[i].y + 15 <= this.y - 4 + 32)) {
              if (this.liver == true) {
                enemyTankBullets[i].liver = false
                clearInterval(enemyTankBullets[i].timer)
              }
              this.liver = false

            }
            break
          case 2:
            if ((this.x + 4 <= enemyTankBullets[i].x + 15 && enemyTankBullets[i].x + 15 <= this.x - 4 + 32) && (this.y + 4 <= enemyTankBullets[i].y + 32 && enemyTankBullets[i].y + 32 <= this.y - 4 + 32)) {
              if (this.liver == true) {
                enemyTankBullets[i].liver = false
                clearInterval(enemyTankBullets[i].timer)
              }
              this.liver = false
            }
            break
          case 3:
            if ((this.x + 4 <= enemyTankBullets[i].x && enemyTankBullets[i].x <= this.x - 4 + 32) && (this.y + 4 <= enemyTankBullets[i].y + 15 && enemyTankBullets[i].y + 15 <= this.y - 4 + 32)) {
              if (this.liver == true) {
                enemyTankBullets[i].liver = false
                clearInterval(enemyTankBullets[i].timer)
              }
              this.liver = false
            }
            break
        }
      }
    }
  }

}

var enemyTankBullet = null
var enemyTankBullets = new Array()
//定义一个敌人坦克类（继承父亲基类）
class EnemyTank extends Tank {
  constructor(x, y, direct, color) {
    super(x, y, direct, color)
    this.liver = true
    this.speed = 1
    this.step = 88
  }
  move() {
    switch (this.direct) {
      case 0:
        super.moveUp()
        break
      case 1:
        super.moveRight()
        break
      case 2:
        super.moveDown()
        break
      case 3:
        super.moveLeft()
        break
    }
  }

  shotBullet() {
    enemyTankBullet = new Bullet(this.x, this.y, this.direct)
    enemyTankBullets.push(enemyTankBullet)
    var timer = window.setInterval("enemyTankBullets[" + (enemyTankBullets.length - 1) + "].run()", 10)
    enemyTankBullets[enemyTankBullets.length - 1].timer = timer
  }

  dead() {
    for (var i = 0; i < heroBullets.length; i++) {
      if (heroBullets[i].liver == true) {
        switch (heroBullets[i].direct) {
          case 0:
            if ((this.x + 4 <= heroBullets[i].x + 15 && heroBullets[i].x + 15 <= this.x - 4 + 32) && (this.y + 4 <= heroBullets[i].y && heroBullets[i].y <= this.y - 4 + 32)) {
              if (this.liver == true) {
                heroBullets[i].liver = false
                clearInterval(heroBullets[i].timer)
              }
              this.liver = false
            }
            break
          case 1:
            if ((this.x + 4 <= heroBullets[i].x + 32 && heroBullets[i].x + 32 <= this.x - 4 + 32) && (this.y + 4 <= heroBullets[i].y + 15 && heroBullets[i].y + 15 <= this.y - 4 + 32)) {
              if (this.liver == true) {
                heroBullets[i].liver = false
                clearInterval(heroBullets[i].timer)
              }
              this.liver = false

            }
            break
          case 2:
            if ((this.x + 4 <= heroBullets[i].x + 15 && heroBullets[i].x + 15 <= this.x - 4 + 32) && (this.y + 4 <= heroBullets[i].y + 32 && heroBullets[i].y + 32 <= this.y - 4 + 32)) {
              if (this.liver == true) {
                heroBullets[i].liver = false
                clearInterval(heroBullets[i].timer)
              }
              this.liver = false
            }
            break
          case 3:
            if ((this.x + 4 <= heroBullets[i].x && heroBullets[i].x <= this.x - 4 + 32) && (this.y + 4 <= heroBullets[i].y + 15 && heroBullets[i].y + 15 <= this.y - 4 + 32)) {
              if (this.liver == true) {
                heroBullets[i].liver = false
                clearInterval(heroBullets[i].timer)
              }
              this.liver = false
            }
            break
        }
      }
    }
  }
}

//定义一个子弹类
class Bullet {
  constructor(x, y, direct) {
    this.x = x
    this.y = y
    this.speed = 1
    this.timer = null
    this.liver = true
    this.direct = direct
  }
  dead() {
    switch (this.direct) {
      case 0:
        if (this.y == 0) {
          return true
        }
        else {
          return false
        }
      case 1:
        if (this.x == 352) {
          return true
        }
        else {
          return false
        }
      case 2:
        if (this.y == 352) {
          return true
        }
        else {
          return false
        }
      case 3:
        if (this.x == 0) {
          return true
        }
        else {
          return false
        }
    }
  }
  run() {
    if (this.dead() == true) {
      clearInterval(this.timer)
      this.liver = false
    }
    else {
      for (var i = 0; i < enemyTanks.length; i++) {
        enemyTanks[i].dead()
      }
      hero.dead()
      switch (this.direct) {
        case 0:
          this.y = this.y - this.speed
          break
        case 1:
          this.x = this.x + this.speed
          break
        case 2:
          this.y = this.y + this.speed
          break
        case 3:
          this.x = this.x - this.speed
          break
      }
    }
  }
}

